<template>
	<view>
		<view class="create">
			<view class="container">
				<view class="u-text-center u-p-t-80 u-p-b-40">
					<view>
						<view class="u-m-t-30 u-font-36">请输入您要新建的客户组(部门)名称</view>
					</view>
				</view>
				<view>
					<u-form :model="form" ref="uForm" label-width="200">
						<u-form-item label="客户组名称">
							<u-input v-model="form.name"/>
						</u-form-item>
						<u-form-item label="所在地区">
							<u-input v-model="form.cityName" type="select" border="border" class="input" @click="cityshow = true"
								:customStyle="{'padding-top': '6rpx','padding-bottom': '6rpx'}"
								placeholder="请选择客户组地区" />
						</u-form-item>
					</u-form>
				</view>
				<view class="u-p-b-30 u-p-t-60">
					<u-button type="primary" size="default" @click="createEnterprise">提交</u-button>
				</view>
				<!-- <view>
					<u-button size="default" @click="back">返回上一页</u-button>
				</view> -->
			</view>
		</view>
		
		<!-- 选择所在地区 -->
		<citypicker ref="citypicker" :show="cityshow" @select="getSelectCity" @close="cityshow = false"></citypicker>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex';
	
	export default {
		data() {
			return {
				form:{},
				// 选择地区
				cityshow: false,
				enterpriseId: 0
			}
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail
			})
		},
		onLoad(opt) {
			if(opt){
				this.enterpriseId = opt.id
			}
		},
		methods: {
			getSelectCity(arr) { //城市选择回调函数
				console.log(arr)
				this.cityshow = false
				// this.selectArea = '';
				let cityidArr = [];
				let getArr = arr.filter(n => n.id != 0)
				let areaname = '';
				getArr.forEach((item, index) => {
					if (index == getArr.length - 1) {
						areaname += item.name
					} else {
						areaname += item.name + '-'
					}
					cityidArr.push(item.id);
				});
				if (cityidArr.length > 0)
					this.form.cityPath = cityidArr.join(',');
					this.form.cityId = cityidArr[cityidArr.length - 1]
				if (areaname)
					// this.form.cityName = areaname;
					this.form.cityName = areaname.split("-")[areaname.split("-").length - 1]
			},
			// 创建客户组(部门)
			createEnterprise(){
				this.$u.post('/Enterprise/Create',[{
					"enterpriseId": this.enterpriseId,
					"name": this.form.name,
					"cityId": this.form.cityId,
					"user": {
						"defaultPassType": 0,
						"password": "",
						"name": this.userdetail.name,
						"mobile": this.userdetail.mobile
					}
				}]).then(data => {
					uni.showModal({
						title: '创建成功',
						success: (res)=> {
							if (res.confirm) {
								uni.navigateBack()
							} else if (res.cancel) {
								
							}
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.create{
		min-height: 100vh;
		background-color: #fff;
	}
	.container{
		background-color: #fff;
		padding: 0 30rpx;
	}
</style>
